# This code replicates Figures 2 and A1

library(dplyr)
library(ggplot2)
library(patchwork)

# Figure 2 ----------------------------------------------------------------

load("data/motivation.Rdata")

# constructs the time series plot of homicides in Colombia and Antioquia
p1 <- monthly_homicide %>%
  filter(date < as.Date("01-01-2019", format = "%m-%d-%Y")) %>%
  mutate(Sample = group) %>%
  ggplot(aes(x = date, y = rate.12, lty = Sample)) + 
  geom_line() + 
  scale_y_continuous("Homicide rate per 100k (last 12 months)", lim = c(0, 180)) + 
  scale_x_date("Date", limits =as.Date(c("01-01-1999", "01-01-2019"), format = "%m-%d-%Y")) + 
  theme_minimal() + 
  theme(legend.position = "bottom", panel.border = element_rect(color = "black", fill = NA)) 

# constructs the time series plot of trust in police in Colombia and Antioquia
p2 <- lb_trust %>%
  ggplot(aes(x = year, y = trust, lty = Sample)) + 
  geom_line() + 
  scale_y_continuous("Mean trust in police (7-point Likert scale)", lim = c(1, 7), breaks = 1:7) + 
  scale_x_continuous("Date", limits = c(1999, 2018), breaks = c(2000, 2005, 2010, 2015, 2020)) + 
  theme_minimal() + 
  theme(legend.position = "bottom", panel.border = element_rect(color = "black", fill = NA),
        panel.grid.minor.y = element_blank()) 

ggsave(p1+p2 + plot_layout(guides = "collect")& theme(legend.position = 'bottom'), file = "results/Figure_2.pdf", width = 9, height = 4)


# Figure A1 ---------------------------------------------------------------

rm(list = ls())

load("data/comm_policing_lit.Rdata")

# bar plot of ngrams
q1 <- filter(ngrams, year > 1970) %>%
  ggplot(aes(x = year, y = ngrams_permillion)) +
  geom_bar(stat = "identity", fill = "gray60", col = "gray40") +
  theme_minimal() + 
  scale_x_continuous("", limits = c(1970, 2023)) +
  ylab("'Community Policing' per 1m Bigrams")

# bar plot of google scholar articles
q2 <- gs %>% 
  ggplot(aes(x = year, y = gs)) +
  geom_bar(stat = "identity", fill = "gray60", col = "gray40") +
  theme_minimal() + 
  xlab("") +
  ylab("Articles Indexed by Google Scholar (1000s)")

# bar plot of community policing evaluations (collected by Blair et al.)
q3 <- sr %>% 
  mutate(year = as.numeric(as.character(year))) %>%
  ggplot(aes(x = year, y = count)) +
  geom_bar(stat = "identity", fill = "gray60", col = "gray40") +
  theme_minimal() + 
  scale_x_continuous("", limits = c(1970, 2023)) +
  ylab("Evaluations of Community Policing")

ggsave(q1+q2+q3, file = "results/Figure_A1.pdf", width = 11, height = 4)
